package com.github.thorqin.toolkit.web.taglib;

import com.github.thorqin.toolkit.Application;
import com.github.thorqin.toolkit.utility.Localization;
import com.github.thorqin.toolkit.web.WebApplication;
import com.github.thorqin.toolkit.web.utility.ServletUtils;
import com.google.common.base.Strings;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

/**
 * Created by thor on 6/12/15.
 */
public class LocaleTag extends SimpleTagSupport {

    private String bundle = null;
    private String locale = null;
	private String appName = null;

    @Override
    public void doTag() throws JspException, IOException {
		WebApplication app = (WebApplication) Application.get(appName);
        HttpServletRequest request = (HttpServletRequest)this.getJspContext()
                .getAttribute("javax.servlet.jsp.jspRequest");
        if (bundle == null) {
            getJspContext().setAttribute("loc", Localization.getInstance());
        } else {
            String currentLocale;
            if (locale == null) {
                String lang = ServletUtils.getLangSetting(app, request);
                if (lang != null)
                    currentLocale = lang;
                else
                    currentLocale = request.getHeader("Accept-Language");
            } else
                currentLocale = locale;
            getJspContext().setAttribute("loc", Localization.getInstance(bundle, currentLocale));
        }
    }

	public void setAppName(String appName) {
		if (Strings.isNullOrEmpty(appName))
			appName = null;
		this.appName = appName;
	}

    public void setBundle(String bundle) {
        if (Strings.isNullOrEmpty(bundle))
            bundle = null;
        this.bundle = bundle;
    }

    public void setLocale(String locale) {
        if (Strings.isNullOrEmpty(locale))
            locale = null;
        this.locale = locale;
    }

}
